package com.lanou.controller;

import com.lanou.bean.Account;
import com.lanou.bean.Service;
import com.lanou.service.AccountService;
import com.lanou.service.CostService;
import com.lanou.service.ServiceService;
import com.lanou.utils.AjaxResult;
import org.apache.shiro.SecurityUtils;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

/**
 * Created by dllo on 18/3/15.
 */
@Controller
public class ServiceController {
    @Resource
    private ServiceService serviceService;
    @Resource
    private AccountService accountService;

    @RequestMapping(value = "/service_list")
    public String service_list(){
        if (SecurityUtils.getSubject().isPermitted("业务账号")){
            return "service/service_list";
        }else {
            return "nopower";
        }
    }
    @RequestMapping(value = "/service_add")
    public String service_add(){
        return "service/service_add";
    }

    @RequestMapping(value = "/service_modi")
    public String service_modi(){
        return "service/service_modi";
    }
    @RequestMapping(value = "/service_detail")
    public String service_detail(){

        return "service/service_detail";
    }

    /**
     * 查询所有的业务信息
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/selectAllService")
    public List<Service> selectAllService(){
        return serviceService.selectAllService();
    }

    /**
     * 根据业务ID查询业务信息
     * @param id
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/selectService")
    public Service selectService(@RequestParam("id") int id){

        return serviceService.selectServiceByServiceId(id);
    }

    /**
     * 根据业务ID修改业务信息
     * @param serviceId
     * @param serviceType
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/updateService")
    public AjaxResult updateService(@RequestParam("serviceId") int serviceId,
                                    @RequestParam("serviceType") int serviceType){
        serviceService.updataService(serviceId,serviceType);
        AjaxResult ajaxResult = new AjaxResult();
        ajaxResult.setMsg("/service_list");
        return ajaxResult;

    }

    /**
     * 根据业务id删除业务信息
     * @param id
     * @return
     */
    @RequestMapping(value = "/delectService")
    public AjaxResult delectService(@RequestParam("id") int id){
        serviceService.delectService(id);
        AjaxResult ajaxResult = new AjaxResult();
        ajaxResult.setMsg("/service_list");
        return ajaxResult;
    }

    /**
     * 修改业务状态暂停,启用和删除
     * @param serviceId
     * @param status
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/updateStatu")
    public AjaxResult updateStatu(@RequestParam("service") int serviceId,@RequestParam("status") String status ){
        if (status.equals("0")){
            status="1";
            serviceService.updateStatu(serviceId,status);
            serviceService.updateDelPauseDate(null,serviceId);
            AjaxResult ajaxResult = new AjaxResult();
            ajaxResult.setMsg("/service_list");
            return ajaxResult;

        }else if (status.equals("1")){
            status="0";
            serviceService.updateStatu(serviceId,status);
            serviceService.updatePauseDate(new SimpleDateFormat("yyyy-MM-dd hh:mm:ss").format(new Date(System.currentTimeMillis())),serviceId);
            AjaxResult ajaxResult = new AjaxResult();
            ajaxResult.setMsg("/service_list");
            return ajaxResult;

        }
        else {
            serviceService.updateStatu(serviceId,status);
            serviceService.updateCloseDate(new SimpleDateFormat("yyyy-MM-dd hh:mm:ss").format(new Date(System.currentTimeMillis())),serviceId);
            AjaxResult ajaxResult = new AjaxResult();
            ajaxResult.setMsg("/service_list");
            return ajaxResult;
        }

    }

    /**
     * 根据身份证号查询账务账号信息
     * @param cardNo
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/selectByCardNo")
    public Account selectByCardNo(@RequestParam("cardNo") String cardNo){
        return accountService.selectByCardId(cardNo);
    }

    /**
     * 添加业务账号信息
     * @param accountId
     * @param costId
     * @param ip
     * @param name
     * @param psw
     * @param psw1
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/insertService")
    public AjaxResult insertService(@RequestParam("accountId") int accountId,
                                    @RequestParam("costId") int costId,
                                    @RequestParam("ip") String ip,
                                    @RequestParam("name") String name,
                                    @RequestParam("psw") String psw,
                                    @RequestParam("psw1") String psw1){
        Service service = new Service();
        service.setAccountId(accountId);
        service.setCostId(costId);
        service.setUnixHost(ip);
        service.setOsUsername(name);
        service.setLoginPasswd(psw);
        service.setStatus("1");
        service.setCreateDate(new SimpleDateFormat("yyyy-MM-dd hh:mm:ss").format(new Date(System.currentTimeMillis())));
        serviceService.insertService(service);
        AjaxResult ajaxResult = new AjaxResult();
        ajaxResult.setMsg("/service_list");
        return ajaxResult;

    }

    /**
     * 根据登录名查询业务账号信息
     * @param osName
     * @param ip
     * @param card
     * @param status
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/selectByOSName")
    public List<Service> selectByOSName(@RequestParam("osName") String osName,
                                        @RequestParam("ip") String ip,
                                        @RequestParam("card") String card,
                                        @RequestParam("status") String status){


        return serviceService.selectByOSName(osName, ip, card, status);
    }

    /**
     * 根据业务账号ID查询业务账号信息
     * @param id
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/selectByServiceId")
   public Service selectByServiceId(@RequestParam("serviceId") int id){
       return serviceService.selectByServiceId(id);
   }



}
